#include "LightingCommon.fxh"

struct VS_IN
{
	float3 posL     : POSITION;
};

struct VS_OUT
{
	float4 posD         : SV_POSITION;
	noperspective float2 texCoord         : TEXCOORD0;
};

VS_OUT VS(VS_IN vIn)
{
	VS_OUT vOut;

	float4 posH = float4(vIn.posL, 1.0f);
	vOut.posD = posH;

	// SV_POSITION interpolator will be in window coords in PS, so another copy in [0;1] is useful
	vOut.texCoord = (posH / posH.w).xy * float2(0.5f, -0.5f) + float2(0.5f, 0.5f);
	
	return vOut;
}

float PS(VS_OUT pIn) : SV_Target
{
	float halfPixelWidth = 0.5f / gGBufferRes.x;
	float halfPixelHeight = 0.5f / gGBufferRes.y;

	
	float depth = 0.0f;

	depth = getDepthFromGBuffer(pIn.texCoord); // the same as pIn.texCoord + float2(halfPixelWidth, halfPixelHeight) !

	//depth = gDepthMap.Sample(gPointSam, pIn.texCoord + float2(-halfPixelWidth, -halfPixelHeight)).r;
	//depth = max(depth, gDepthMap.Sample(gPointSam, pIn.texCoord + float2(halfPixelWidth, -halfPixelHeight)).r);
	//depth = max(depth, gDepthMap.Sample(gPointSam, pIn.texCoord + float2(-halfPixelWidth, halfPixelHeight)).r);
	//depth = max(depth, gDepthMap.Sample(gPointSam, pIn.texCoord + float2(halfPixelWidth, halfPixelHeight)).r);

	//depth += gDepthMap.Sample(gBilinearSam, pIn.texCoord + float2(-halfPixelWidth, -halfPixelHeight)).r;
	//depth += gDepthMap.Sample(gBilinearSam, pIn.texCoord + float2( halfPixelWidth, -halfPixelHeight)).r;
	//depth += gDepthMap.Sample(gBilinearSam, pIn.texCoord + float2(-halfPixelWidth,  halfPixelHeight)).r;
	//depth += gDepthMap.Sample(gBilinearSam, pIn.texCoord + float2( halfPixelWidth,  halfPixelHeight)).r;
//
	//depth *= 0.25f;

	//for (float i = -3.5f; i <= 3.5f; i+=1.0f) {
		//for (float j = -3.5f; j <= 3.5f; j+=1.0f) {
			//float2 offset = float2(1.0f / 1024.0f * float(j), 1.0f / 600.0f * float(i));
			//result += gSSAOMap.Sample(gPointSam, pIn.texCoord + offset).r;
		//}
	//}
	//
	//result *= 0.0625 * 0.25;
	return depth;
}

technique11 tech
{
	pass P0
	{
		SetVertexShader( CompileShader( vs_5_0, VS() ) );
		SetGeometryShader( NULL );
		SetPixelShader( CompileShader( ps_5_0, PS() ) );
		
		SetDepthStencilState(DepthOff, 0);
		SetRasterizerState(CullBack);
	}
}


